package cn.lx.ssh.action;


import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.lx.ssh.domain.Product;
import cn.lx.ssh.service.ProductService;

/**
 * 商品管理的Action层
 * @author issuser
 *
 */
public class ProductAction extends ActionSupport implements ModelDriven<Product>{
	
	//模型驱动使用的类
	private Product product = new Product();
	@Override
	public Product getModel() {
		// TODO Auto-generated method stub
		return product;
	}
	
	//struts 和 spring 整合过程中按名称自动注入业务层的类
	private ProductService productService;
	public void setProductService(ProductService productService) {
		this.productService = productService;
	}

	
	public String save(){
		System.out.println("action中的save方法执行啦");
		productService.save(product);
		
		
		return SUCCESS;
	}
	
	
	public String list(){
		List<Product> list = productService.getList();
		System.out.println(list.get(0).getPrice());
		/*HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("productList",list);
		*/
		ActionContext.getContext().put("productList", list);
		return "list";
	}
	
	
	public String edit(){
		
		//Integer pid=(Integer) ActionContext.getContext().getContextMap().get("pid");
		HttpServletRequest request = ServletActionContext.getRequest(); 
		String pid=(String) request.getParameter("pid");
		Product product = productService.findById(Integer.valueOf(pid));
		ActionContext.getContext().put("product", product);
		return "edit";
	}
	
	public String update(){
		System.out.println("action中的save方法执行啦");
		productService.save(product);
		
		
		return NONE;
	}

	
}
